#include "e2d/core/physics/PxShapeMap.h"
#include "e2d/core/physics/PxUtils.h"

void e2d::PxBasicMap::CreatMaterial()
{
	m_Material = e2d::PxUtils::gPhysics->createMaterial(m_MaterialData.m_StaticFriction, m_MaterialData.m_DynamicFriction, m_MaterialData.m_Restitution);
}

void e2d::PxBasicMap::SetLocalPos()
{
	if (m_Shape)
	{
		m_Shape->setLocalPose(e2d::PxUtils::toPxTransform(m_LocalPos));
	}
}

void e2d::PxBasicMap::DeleteShape()
{
	if (m_Shape)
	{
		m_Shape->release();
		m_Shape = 0;

		
	}
}

void e2d::PxBasicMap::DeleteMaterial()
{
	if (m_Material)
	{
		m_Material->release();
		m_Material = 0;
	}
}

void e2d::PxBasicMap::Delete()
{
	DeleteShape();
	DeleteMaterial();
}

void e2d::PxBoxShapeMap::CreateShape()
{
	physx::PxBoxGeometry boxGeo(PxUtils::toPxVec3(m_Box.m_HalfExtends));
	m_Shape = PxUtils::gPhysics->createShape(boxGeo, *m_Material);
	e2d::PxBasicMap::SetLocalPos();
}

void e2d::PxShapeMap::Create()
{
	for (auto& it : m_BoxShapes)
	{
		it.CreatMaterial();
		it.CreateShape();
	}
}


bool e2d::PxShapeMap::AttachedToRigidBody(physx::PxRigidActor* rb)
{
	if (rb == 0)
	{
		return false;
	}

	for (auto& it : m_BoxShapes)
	{
		rb->attachShape(*it.m_Shape);
	}
	return true;
}

bool e2d::PxShapeMap::DetachedToRigidBody(physx::PxRigidActor* rb)
{
	if (rb == 0)
	{
		return false;
	}

	for (auto& it : m_BoxShapes)
	{
		rb->detachShape(*it.m_Shape);
	}
	return true;
}

void e2d::PxShapeMap::Delete()
{
	for (auto& it : m_BoxShapes)
	{
		it.Delete();
	}
}
